package pos.data;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import empleados.Vacaciones;

public class JDBCVacacionesDAO implements IVacacionesDAO{
	
private Connection conn;
	
	public JDBCVacacionesDAO(){
		
		conn = ConnectionManager.getInstance().checkOut();
		
		
	}

	@Override
	public void delete(String dni, Date fechaini) {
		String sql = "DELETE FROM vacaciones WHERE (dniemp = ? AND fechaini = ?) ";
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, dni);
            stmt.setDate(2, fechaini);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
		
	}


	@Override
	public void insert(Vacaciones v) {
		String sql = "INSERT INTO vacaciones (dniemp, fechaini, fechafin, estado) VALUES (?, ?, ?, ?) ";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);

			stmt.setString(1, v.getDNIemp());
			stmt.setDate(2, v.getFechaIni());
			stmt.setDate(3, v.getFechaFin());
			stmt.setString(4, "por confimar");
			stmt.executeUpdate();

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				conn.close();
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		
	}


	@Override
	public void modificaestadovacaciones(Vacaciones v, String estado) {
		PreparedStatement stmt = null;
		ResultSet result = null;
		String sql = "UPDATE vacaciones SET estado = ?"+
		"WHERE  (dniemp = ? AND fechaini = ?)";

		try{
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, estado);
			stmt.setString(2, v.getDNIemp());
			stmt.setDate(3, v.getFechaIni());
			stmt.executeUpdate();
		}catch(SQLException e){
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				conn.close();
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		
	}


	@Override
	public Vacaciones select(String dni, Date fechaini) {
		PreparedStatement stmt = null;
        ResultSet result = null;        
        Vacaciones v = null;       
        String sql = "SELECT * FROM vacaciones WHERE (dniemp = ? AND fechaini = ?) ";
        try {
            stmt = conn.prepareStatement(sql);            
            stmt.setString(1, dni);
            stmt.setDate(2, fechaini);
            result = stmt.executeQuery();

            if(result.next()){
            	v = new Vacaciones();
        		v.setDNIemp(result.getString("dniemp"));
            	v.setFechaIni(result.getDate("fechaini"));
            	v.setFechaFin(result.getDate("fechafin"));
            	v.setEstado(result.getString("estado"));           	
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return v;
	}


	@Override
	public List<Vacaciones> selectvacacionesempleado(String dni) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        List<Vacaciones> listavacaciones = new LinkedList<Vacaciones>();
       
        String sql = "SELECT * FROM vacaciones WHERE (dniemp = ?) ORDER BY fechaini";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, dni);
            result = stmt.executeQuery();
            while(result.next()){
            	Vacaciones v = new Vacaciones();
        		v.setDNIemp(result.getString("dniemp"));
            	v.setFechaIni(result.getDate("fechaini"));
            	v.setFechaFin(result.getDate("fechafin"));
            	v.setEstado(result.getString("estado"));
            	listavacaciones.add(v);
            	
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return listavacaciones;
	}


	@Override
	public List<Vacaciones> selectvacacionesporestado(String estado) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        List<Vacaciones> listavacaciones = new LinkedList<Vacaciones>();       
        String sql = "SELECT * FROM vacaciones WHERE (estado = ?) ";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, estado);
            result = stmt.executeQuery();
 
            while(result.next()){
            	Vacaciones v = new Vacaciones();
        		v.setDNIemp(result.getString("dniemp"));
            	v.setFechaIni(result.getDate("fechaini"));
            	v.setFechaFin(result.getDate("fechafin"));
            	v.setEstado(result.getString("estado"));
            	listavacaciones.add(v);
            	
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return listavacaciones;
	}

	
	
	public void finalize(){
		ConnectionManager.getInstance().checkIn(conn);
		
	}

}
